package com.salinity.kun.helper;

import java.text.DecimalFormat;

import Jama.Matrix;

public class MatrixHelper {

	public static boolean equals(Matrix m1,Matrix m2) {
		int i,j,r1,r2,c1,c2;
		r1=m1.getRowDimension();
		r2=m2.getRowDimension();
		c1 = m1.getColumnDimension();
		c2=m2.getColumnDimension();
		if(r1!=r2||c1!=c2) {
			return false;
		}
		
		for(i=0;i<r1;i++) {
			for(j=0;j<c1;j++) {
				if(m1.get(i, j)!=m2.get(i, j)) {
					return false;
				}
			}
		}
		
		return true;
	}
	
	
	public static void printMatrix(double[][] matrix) {
		System.out.println();
		System.out.println(getMatrixAsString(matrix).toString());
	}
	
	public static void printMatrix(double[][] matrix,String splitChar) {
		System.out.printf(getMatrixAsString(matrix,splitChar).toString());
	}

	public static StringBuilder getMatrixAsString(double[][] matrix,String splitChar) {
		int m, n, i, j;
		DecimalFormat df = new DecimalFormat("#0.0000000000000");
		StringBuilder sb = new StringBuilder();
		m = matrix.length;
		if (m >= 0) {
			n = matrix[0].length;
		} else {
			n = 0;
		}
		for (i = 0; i < m; i++) {
			for (j = 0; j < n; j++) {
				sb.append(df.format(matrix[i][j])).append(splitChar);
			}
			sb.append("\n");
		}
		return sb;
	}
	
	public static StringBuilder getMatrixAsString(double[][] matrix) {
		return getMatrixAsString(matrix,"\t");

	}
	
}
